package com.interauto.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.interauto.Dao.entity.Interinfo;
import com.interauto.Dao.entity.Interunt;
import com.interauto.bean.AutoAssInfoBean;
import com.interauto.bean.AutoAssertInfoBean;
import com.interauto.bean.AutoInterCaseBean;
import com.interauto.bean.autoenum.AutoExpectEnum;
import com.interauto.bean.autoenum.HttpEnum;
import com.interauto.bean.httpinfo.HttpRequestBean;
import com.interauto.bean.httpinfo.HttpResPonseBean;
import com.interauto.bean.web.AssinfoWeb;
import com.interauto.bean.web.CheckInfoWeb;
import com.interauto.bean.web.InterinfoWeb;
import com.interauto.bean.web.InterruntWeb;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import static com.interauto.bean.autoenum.HttpEnum.HttpMeth.getHttpMeth;
import static com.interauto.bean.autoenum.HttpEnum.HttpType.getHttpType;

/**
 * 提供给在线调试用的哈
 */
@Component
public class AutoDebugRunInterService {
    private static final Logger logger= LoggerFactory.getLogger(AutoDebugRunInterService.class);
    @Autowired
    private AutoRunResultService autoRunResultService;
    @Autowired
    private AutoRunCaseinfoService autoRunCaseinfoService;
    @Autowired
    private AutoCaseBeanService autoCaseBeanService;


    public InterruntWeb debugruninterinfo(InterinfoWeb interinfoWeb){
        if(interinfoWeb == null){
            return null;
        }
        logger.info("debugruninterinfo info is -->"+interinfoWeb.toString());
        AutoInterCaseBean autoInterCaseBean=getAutoInterCaseBean(interinfoWeb);
        HttpResPonseBean httpResPonseBean=new HttpResPonseBean();
        AutoAssertInfoBean autoAssertInfoBean=autoRunCaseinfoService.runinterinfo(autoInterCaseBean,httpResPonseBean);
        Interunt interunt=autoRunResultService.getInterunt(autoInterCaseBean,
                httpResPonseBean,
                autoAssertInfoBean.getErrormessage(),interinfoWeb.getCaseid(),
                autoAssertInfoBean.getCodetype());
        InterruntWeb interruntWeb=new InterruntWeb();
        try{
            BeanUtils.copyProperties(interunt,interruntWeb);
            String checkinfostr=interunt.getAutoassinfo();  //主要是处理给前端用的。将校验值转换为对象
            if(StringUtils.isNoneEmpty(checkinfostr)){
                List<CheckInfoWeb> checkInfoWebList= JSON.parseObject(checkinfostr,new TypeReference<List<CheckInfoWeb>>(){});
                interruntWeb.setCheckInfoWebs(checkInfoWebList);
            }
            System.out.println("--->>>>>"+autoAssertInfoBean.toString());
        }
        catch (Exception e){
            logger.error("copyProperties is system error;interunt is-->"+interunt.toString());
        }

        return interruntWeb;
    }


    /**
     * 将前端传的在线调试的对象，转换为后端可执行的对象
     * @param interinfoWeb 前端传的对象
     * @return 后台可执行的对象
     */
    private AutoInterCaseBean getAutoInterCaseBean(InterinfoWeb interinfoWeb){
        AutoInterCaseBean autointer=new AutoInterCaseBean();
        autointer.setInterfacename(interinfoWeb.getInterfacename());
        autointer.setIntercasetype(interinfoWeb.getIntercasetype());
        autointer.setChecktype(interinfoWeb.getChecktype());
        List<AssinfoWeb> assinfoWebs=interinfoWeb.getAssinfoWebs();
        if(assinfoWebs !=null && assinfoWebs.size()>0){
            ArrayList<AutoAssInfoBean> autoAssInfoBeans=new ArrayList<>();
            for(AssinfoWeb assinfoWeb:assinfoWebs){
                String checkinfo=assinfoWeb.getCheckinfo();
                if(StringUtils.isEmpty(checkinfo)){
                    continue;
                }
                autoAssInfoBeans.add(new AutoAssInfoBean(checkinfo, assinfoWeb.getExpectinfo(),
                        AutoExpectEnum.getAutoExpectEnum(assinfoWeb.getAutotype())));
            }
            autointer.setAutoAssInfoBeans(autoAssInfoBeans);
        }
        try{
            Interinfo interinfo=new Interinfo();
            BeanUtils.copyProperties(interinfoWeb,interinfo);
            interinfo.setHeadstrmap(interinfoWeb.getHeadstr());
            HttpRequestBean httpRequestBean=autoCaseBeanService.getHttpRequestBean(interinfo);
            autointer.setHttpRequestBean(httpRequestBean);
        }
        catch (Exception e){
            logger.error("copyProperties is system error --->interinfoWeb:"+interinfoWeb.toString());
        }
        return autointer;
    }
}
